
package kaizone.haining.chaobohui.widget;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.util.ImageFetcher;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DetailImageGallery extends LinearLayout {

    private ImageView mImageView;

    private Gallery mGallery;

    private ImageFetcher mImageFetcher;

    private List<PicModel> mData;

    private MyAdapter mAdapter;

    private OnImageItemClickListener mImageItemClickListener;

    private int mGalleryItemWidth;

    public DetailImageGallery(Context context) {
        super(context);
        init();
    }

    public DetailImageGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mData = new ArrayList<PicModel>();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mImageView = (ImageView) findViewById(R.id.img);
        mGallery = (Gallery) findViewById(R.id.gallery);
        initWidget();
    }

    private void initWidget() {
        mAdapter = new MyAdapter();
        mGallery.setAdapter(mAdapter);
        // mGallery.scrollBy(100, 0);
        mGallery.setSelection(1);
        mGallery.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mImageItemClickListener != null) {
                    if (mData == null || mData.isEmpty())
                        return;
                    if (position < 0 || position >= mData.size())
                        return;
                    PicModel picmodel = mData.get(position);
                    if (picmodel == null)
                        return;
                    mImageItemClickListener.onItemClick(parent, view, position, id, picmodel.url);
                }
            }
        });
        mGallery.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (mAdapter == null)
                            return;
                        final int columnWidth = mGallery.getWidth() * 1 / 4;
                        final int columnHeight = columnWidth;
                        mAdapter.setItemRange(columnWidth, columnHeight);

                    }
                });
    }

    public void setData(List<PicModel> data) {
        if (mData != null) {
            mData.removeAll(data);
            mData.addAll(data);
            mAdapter.notifyDataSetChanged();
        }
    }

    public ImageFetcher getmImageFetcher() {
        return mImageFetcher;
    }

    public void setImageFetcher(ImageFetcher mImageFetcher) {
        this.mImageFetcher = mImageFetcher;
    }

    public void setImageItemClickListener(OnImageItemClickListener listener) {
        mImageItemClickListener = listener;
    }

    private class MyAdapter extends BaseAdapter {

        private int mOver = 3;

        private int mItemWidth = 0;

        private Gallery.LayoutParams mImageViewLayoutParams;

        public MyAdapter() {
            mImageViewLayoutParams = new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
        }

        @Override
        public int getCount() {
            int count = mOver;
            if (mData != null && !mData.isEmpty()) {
                int size = mData.size();
                count = size > mOver ? size : mOver;
            } else {
                count = mOver;
            }
            return count;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, instantiate and
                                       // initialize
                imageView = new ImageView(getContext());
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setLayoutParams(mImageViewLayoutParams);
                imageView.setImageResource(R.drawable.empty_photo);
            } else { // Otherwise re-use the converted view
                imageView = (ImageView) convertView;
            }
            if (mData != null && !mData.isEmpty()) {
                final int size = mData.size();
                if (position < size) {
                    PicModel picModel = mData.get(position);
                    if (picModel != null) {
                        String sum100x000 = picModel.sum100x000;
                        mImageFetcher.loadImage(sum100x000, imageView);
                    }
                }
            }
            if (imageView.getLayoutParams().width != mGalleryItemWidth) {
                imageView.setLayoutParams(mImageViewLayoutParams);
            }
            return imageView;
        }

        public void setItemRange(int width, int height) {
            if (width == mItemWidth) {
                return;
            }
            mItemWidth = width;
            mImageViewLayoutParams = new Gallery.LayoutParams(mItemWidth, mItemWidth);
            notifyDataSetChanged();
        }
    }

    public interface OnImageItemClickListener {
        void onItemClick(AdapterView<?> parent, View view, int position, long id, String imageUrl);
    }

}
